package com.ktgj.train.controller;

import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.train.domain.InterfaceEntity.*;
import com.ktgj.train.domain.TrainAccount;
import com.ktgj.train.domain.VerificationStatus;
import com.ktgj.train.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@ControllerAdvice
@RestController
@RequestMapping("/train")
public class TrainController extends BaseController {

    @Autowired
    private TrainService trainService;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

//    @PostMapping("/orderPayment")
//    public AjaxResult orderPayment(HttpServletRequest httpServletRequest, @RequestBody TrOutTicketRequest trOutTicketRequest, String callback) {
//        return AjaxResult.success(trainService.OrderPayment(httpServletRequest, trOutTicketRequest, callback));
//    }

    @PostMapping("/passengerList")
    public TrContactQueryResponse passengerList(HttpServletRequest httpServletRequest, TrContactQueryRequest trContactQueryRequest) {
        TrContactQueryResponse trContactQueryResponse = trainService.passengerList(httpServletRequest, trContactQueryRequest);
        if (trContactQueryResponse != null) {
            return trContactQueryResponse;
        } else {
            return null;
        }
    }

    /**
     * 添加或修改乘车人
     * @param httpServletRequest
     * @param trAddAndUpdateContactsRequest
     * @return
     */
    @PostMapping("/addUpdatePassenger")
    public AjaxResult addUpdatePassenger(HttpServletRequest httpServletRequest, @RequestBody TrAddAndUpdateContactsRequest trAddAndUpdateContactsRequest) {
        TrAddAndUpdateContactsResponse trAddAndUpdateContactsResponse = trainService.addUpdatePassenger(httpServletRequest, trAddAndUpdateContactsRequest);
        return AjaxResult.success(trAddAndUpdateContactsResponse);
    }

    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody TrainPaymentRequest trainPaymentRequest) {
        Map<String, Object> pay = trainService.pay(trainPaymentRequest);
        if (pay != null) {
            return AjaxResult.success(pay);
        } else {
            return AjaxResult.error("获取失败");
        }
    }

    //找回密码（获取验证码）
    @PostMapping("/retrievePassword")
    public AjaxResult retrievePassword(@RequestBody TrRetrievePasswordRequest request) {
        TrRetrievePasswordResponse response = trainService.retrievePassword(request);
        return AjaxResult.success(response);
    }

    //找回密码（回填验证码）
    //@SignatureValidation
    @PostMapping("/sendcaptcharesult")
    public AjaxResult sendcaptcharesult(HttpServletRequest servletRequest, @RequestBody TrRetrievePasswordRequest request) {
        String token = null;
        if (request.customerId == null || request.customerId == 0){
            token = servletRequest.getHeader("token");
        }
        TrRetrievePasswordResponse response = trainService.sendcaptcharesult(token, request);
        return AjaxResult.success(response);
    }

    //重置密码（获取验证码）
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody TrResetPasswordRequest request) {
        TrResetPasswordResponse response = trainService.resetPassword(request);
        return AjaxResult.success(response);
    }

    //重置密码（发送验证码）
    @PostMapping("/resetPasswordSend")
    public AjaxResult resetPasswordSend(HttpServletRequest servletRequest, TrResetPasswordRequest request) {
        String token = servletRequest.getHeader("token");
        TrResetPasswordResponse response = trainService.resetPasswordSend(token, request);
        return AjaxResult.success(response);
    }

    /**
     * 12306注册
     * @param request
     * @return
     */
    @PostMapping("/registerAccount")
    public AjaxResult registerAccount(@RequestBody TrRegisterAccountRequest request) {
        TrRegisterAccountResponse response = trainService.registerAccount(request);
        return AjaxResult.success(response);
    }

    //短信验证
    //@SignatureValidation
    @PostMapping("/phoneMessageValidate")
    public AjaxResult phoneMessageValidate(HttpServletRequest servletRequest, @RequestBody TrPhoneMessageValidateRequest request) {
        String token = servletRequest.getHeader("token");
        try {
            TrPhoneMessageValidateResponse response = null;
            if (token != null) {
                R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
                response = trainService.phoneMessageValidate(customer.getData().getCustomerId(), request);
            } else {
                response = trainService.phoneMessageValidate(Long.valueOf(request.customerId), request);
            }
            return AjaxResult.success(response);
        } catch (Exception e){
            return AjaxResult.expired("登录已过期，请重新登录");
        }
    }

    @PostMapping("/removeContacts")
    public AjaxResult removeContacts(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String token;
        Long commonTrvelersId;
        try {
            token = request.getHeader("token");
            commonTrvelersId = Long.valueOf(map.get("commonTrvelersId").toString());
        } catch (Exception e){
            return AjaxResult.error("缺少必要参数");
        }
        String traveler;
        try {
            traveler = remoteCustomerService.delFrequentTraveler(token, commonTrvelersId);
            if (traveler != null && !traveler.equals("") && Long.valueOf(traveler) > 0){
                return AjaxResult.success("删除成功");
            }
            else {
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取会员12306账号密码
     * @param request
     * @return
     */
    //@SignatureValidation
    @PostMapping("/queryAccountId")
    public AjaxResult queryAccountId(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String token = request.getHeader("token");
        try {
            TrainAccount trainAccount = null;
            if (token != null) {
                R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
                trainAccount = trainService.queryAccountId(customer.getData().getCustomerId());
            } else {
                Long customerId = Long.valueOf(map.get("customerId").toString());
                trainAccount = trainService.queryAccountId(customerId);
            }

            return AjaxResult.success(trainAccount);
        } catch (Exception e){
            return AjaxResult.expired("登录已过期，请重新登录");
        }
    }

    /**
     * 获取会员12306账号密码(CRM)
     */
//    @SignatureValidation
    @PostMapping("/queryAccountIdCrm")
    public TrainAccount queryAccountIdCrm(@RequestBody Map<String, Object> map) {

        try {
            TrainAccount trainAccount = null;
                Long customerId = Long.valueOf(map.get("customerId").toString());
                trainAccount = trainService.queryAccountId(customerId);


            return trainAccount;
        } catch (Exception e){
            return null;
        }
    }

    @PostMapping("/verificationStatus")
    public AjaxResult verificationStatus(@RequestBody VerificationStatus status) {
        return AjaxResult.success(trainService.verificationStatus(status));
    }
}
